package com.example.maili.woxiamg.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.maili.woxiamg.R;
import com.example.maili.woxiamg.adapter.moreadapter.MoreNewAdapter;
import com.example.maili.woxiamg.api.API;
import com.example.maili.woxiamg.base.BaseActivity;
import com.example.maili.woxiamg.bean.MoreNewBean;
import com.example.maili.woxiamg.utils.AppUtils;
import com.example.maili.woxiamg.utils.OkHttpClientManager;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

public class MoreNewActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView recyclerView;
    private int page = 1;
    private List<MoreNewBean.ListBean> list = new ArrayList<>();
    private MoreNewAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    public void initView() {
        setContentView(R.layout.activity_more_new);
        recyclerView = (RecyclerView) findViewById(R.id.more_new_recycler);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.more_new_swipe);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MoreNewAdapter(R.layout.more_new_item_layout, list, this);
        recyclerView.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(this);

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
                adapter.setEnableLoadMore(true);
            }
        }, recyclerView);
        // 当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
        adapter.setPreLoadNumber(3);
    }

    @Override
    public void initdata() {
        getData();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    getData();
                    break;
            }
        }
    };

    private void getData() {
        OkHttpClientManager.getAsyn(API.MORE_NEWS + page, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                adapter.loadMoreFail();
            }

            @Override
            public void onResponse(String response) {
                Gson getgson = AppUtils.getgson();
                MoreNewBean moreNewBean = getgson.fromJson(response, MoreNewBean.class);
                if (moreNewBean.getList() == null) {
                    AppUtils.toast(context, "数据已加载完");
                    adapter.loadMoreEnd();
                    return;
                }
                list.addAll(moreNewBean.getList());
                adapter.setNewData(list);
                adapter.loadMoreComplete();
                swipeRefreshLayout.setRefreshing(false);//false  刷新完成
            }
        });
    }

    @Override
    public void onRefresh() {
        page = 1;
        adapter.setEnableLoadMore(false);
        handler.sendEmptyMessage(1);
    }
}
